<template>
	<!-- 首页效果 -->
	<view v-if="isHomeStyle">
		<!-- 微信小程序 -->
		<!-- #ifdef MP-WEIXIN -->
		<view class="navBox" :style="{height:navHeight+'rpx',backgroundColor:navBackgroundColor}">
			<view class="navWrap" :style="{height:navigationBarHeight+'rpx',top:statusBarHeight+'rpx'}">
				<!-- 地区 -->
				<view class="position" :style="{height:customHeight+'rpx',lineHeight:customHeight+'rpx',paddingLeft:paddingRight+'rpx'}">
					<view class="iconfont icon-dingwei"></view>
					<text>中东地区</text>
					<view class="iconfont icon-youbian"></view>
				</view>
				<!-- 搜索 -->
				<navigator url="/pages/search/index" class="search" 
					:style="{height:customHeight+'rpx',lineHeight:customHeight+'rpx',marginRight:marginRight+'rpx',fontSize:fontSize+'rpx'}">
					找医院
				</navigator>
			</view>
		</view>
		<!-- #endif -->
		<!-- 非微信小程序之外的其他设备 -->
		<!-- #ifndef MP-WEIXIN -->
		<view class="navBox" :style="{height:statusBarHeight+80+'rpx',backgroundColor:navBackgroundColor}">
			<view :style="{height:80+'rpx',top:statusBarHeight+'rpx'}" style="box-sizing: border-box;display: flex;align-items: center;position:fixed;padding: 10rpx 20rpx;width: 100%;">
				<!-- 地区 -->
				<view style="display: flex;align-items: center;height: 100%;">
					<view class="iconfont icon-dingwei"></view>
					<text>中东地区</text>
					<view class="iconfont icon-youbian"></view>
				</view>
				<!-- 搜索 -->
				<view style="flex: 1;padding-left: 20rpx;">
					<input @click="navigatorTo" style="height: 60rpx;line-height: 60rpx;background-color: #f4f4f4;text-align: center;border-radius: 60rpx;" placeholder="找医院"/>
				</view>
			</view>
		</view>
		<!-- #endif -->
	</view>
	
	<!-- 非首页效果 -->
	<view v-else>
		<!-- 微信小程序 -->
		<!-- #ifdef MP-WEIXIN -->
		<view class="navBox" :style="{height:navHeight+'rpx',backgroundColor:navBackgroundColor}">
			<view class="navWrap" :style="{height:navigationBarHeight+'rpx',top:statusBarHeight+'rpx'}">
				<!-- 返回按钮 -->
				<!-- [z-index: 999;] 处理backBtn类由于父级元素flex导致点击事件失效 -->
				<view class="backBtn" style="z-index: 999;" :style="{height:80+'rpx',lineHeight:80+'rpx',paddingLeft:paddingRight+'rpx',color:titleColor}">
					<view v-if="pages > 1" class="iconfont icon-shangyiyehoutuifanhui" @click="backOrhome"></view>
					<view v-else class="iconfont icon-shouye"></view>
				</view>
				<!-- 文本区 -->
				<view style="width: 100%;text-align: center;position: fixed;left: 0; "
					:style="{height:80+'rpx',lineHeight:80+'rpx',fontSize:fontSizeTitle+'rpx',color:titleColor}">
					{{navTitle}}
				</view>
			</view>
		</view>
		<!-- #endif -->
			 
		<!-- 非微信小程序 -->
		<!-- #ifndef MP-WEIXIN -->
		<view class="navBox" :style="{height:statusBarHeight+90+'rpx',backgroundColor:navBackgroundColor}">
			<view :style="{height:80+'rpx',top:statusBarHeight+'rpx'}" style="display: flex;align-items: center;position: fixed;width: 100%;padding: 2rpx 20rpx;">
				<!-- 返回按钮 -->
				<!-- [z-index: 999;] 处理backBtn类由于父级元素flex导致点击事件失效 -->
				<view class="backBtn" style="z-index: 999;" :style="{height:80+'rpx',lineHeight:80+'rpx',paddingLeft:paddingRight+'rpx',color:titleColor}">
					<view v-if="pages > 1" class="iconfont icon-shangyiyehoutuifanhui" @click="backOrhome"></view>
					<view v-else class="iconfont icon-shouye"></view>
				</view>
				<!-- 文本区 -->
				<view style="width: 100%;text-align: center;position: fixed;left: 0; "
					:style="{height:80+'rpx',lineHeight:80+'rpx',fontSize:fontSizeTitle+'rpx',color:titleColor}">
					{{navTitle}}
				</view>
			</view>
		</view>
		<!-- #endif -->
	</view>
</template>
 
<script>
	import {systemInfo} from '@/common/system.js'
	export default {
		name: "Nav",
		props: {
			//搜索字体大小
			fontSize: {
				type: Number, 
				default: 32 //rpx
			},
			//标题字体大小
			fontSizeTitle:{
				type: Number,
				default: 32 //rpx
			},
			//导航栏整体背景颜色
			navBackgroundColor: {
				type: String,
				default: '#42B7FB'
			},
			//标题字体颜色
			titleColor: {
				type: String,
				default: '#ffffff',
			},
			/******** h5端，app端需要传入自定义导航栏高度 *******/
			navHeightValue: {
				type: Number,
				default: 44 //px
			},
			//是否首页效果
			isHomeStyle:{
				type:Boolean,
				default:false
			},
			//标题
			navTitle:{
				type:String,
				default:''
			}
		},
		computed: {
		
		},
		data() {
			return {
				statusBarHeight: 0, //状态栏高度
				navHeight: 0, //头部导航栏总体高度
				navigationBarHeight: 0, //导航栏高度
				customHeight: 0, //胶囊高度
				scaleFactor: 0, //比例系数
				menubarLeft: 0, //胶囊定位的左边left
				windowWidth: 0,
				pages:getCurrentPages().length, //页面栈数量
				marginRight:0, //搜索框距离胶囊距离
				paddingRight:0, //胶囊右侧距离屏幕右侧距离
			};
		},
		methods: {
			//返回
			backOrhome() {
				 console.log('点击事件被触发');
				if (this.pages > 1) {
					uni.navigateBack()
				} else {
					uni.switchTab({
						url:'/pages/index/index' 
					})
				}
			},
			//跳转搜索
			navigatorTo() {
				uni.navigateTo({
					url:'/pages/search/index'
				})
			}
		},
		created() {
			/* 获取设备信息 */
			const SystemInfomations = systemInfo()
			/* 通用平台 */
			this.statusBarHeight = SystemInfomations.statusBarHeight //状态栏高度
			this.scaleFactor = SystemInfomations.scaleFactor //比例系数
			this.windowWidth = SystemInfomations.windowWidth //当前设备的屏幕宽度
			/* 微信小程序平台 */
			// #ifdef MP-WEIXIN
			this.navHeight = SystemInfomations.navHeight + SystemInfomations.statusBarHeight //头部导航栏总高度
			this.navigationBarHeight = SystemInfomations.navHeight //头部导航栏高度
			this.customHeight = SystemInfomations.menuButtonHeight //胶囊高度
			this.menubarLeft = SystemInfomations.menuButtonLeft //胶囊左边界距离左上角的距离
			this.paddingRight = SystemInfomations.windowWidth - SystemInfomations.menuButtonRight //屏幕宽度 - 胶囊右侧到屏幕左边距离
			this.marginRight = this.paddingRight + SystemInfomations.menuButtonWidth + 12 //12配合 search 元素左边距 实现搜索按钮左右同距
			// #endif
		}
	}
</script>
 
<style>
	/* #ifdef MP-WEIXIN */
	.navBox {
		box-sizing: border-box;
		width: 100%;
		position: fixed;
		top: 0;
		left: 0;
		/* background-image: url(''); */
	}
	.navWrap{
		position: absolute;
		left: 0;
		/* background-color: gray; */
		display: flex;
		align-items: center;
		width: 100%;
	}
	.position{
		display: flex;
		/* background-color: red; */
		align-items: center;
	}
	.search{
		border-radius:200rpx;
		text-align:center;
		background:#f4f4f4;
		margin-left:12rpx;
		/* [flex: 1] 搭配父级flex实现占满右侧（同时注意左右间距） */
		flex: 1;
	}
	/* #endif */
</style>